/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2012-2016 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "fvCFD.H"

#include "volFields.H"
#include "fvcGrad.H"
#include "tensor.H"
#include "symmTensor.H"
#include "transform.H"
#include "stringList.H"
#include "IOstreams.H"

// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

template<class Type>
void Foam::functionObjects::uv_Budget::processField
(
    const word& fieldName,
    const tmp<GeometricField<Type, fvPatchField, volMesh>>& tvalue
)
{
    typedef GeometricField<Type, fvPatchField, volMesh> FieldType;

    const word scopedName = word("uv_") + fieldName;

    if (obr_.foundObject<FieldType>(scopedName))
    {
        FieldType& fld =
            const_cast<FieldType&>(obr_.lookupObject<FieldType>(scopedName));
        fld == tvalue();
    }
    else if (obr_.found(scopedName))
    {
        WarningInFunction
            << "Cannot store turbulence field " << scopedName
            << " since an object with that name already exists"
            << nl << endl;
    }
    else
    {
      obr_.store
        (
            new FieldType
            (
                IOobject
                (
                    scopedName,
                    obr_.time().timeName(),
                    obr_,
                    IOobject::READ_IF_PRESENT,
                    IOobject::NO_WRITE
                ),
                tvalue
            )
        );
    }
}


// returns the convection term 对流项 C_k
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::uv_Budget::Ck() const
{
const volVectorField& U = obr_.lookupObject<volVectorField>("UMean");//获取速度场U

  const volSymmTensorField& UP2M = obr_.lookupObject<volSymmTensorField>("UPrime2Mean");//获取雷诺应力矩阵，也就是RMS
 
  const volScalarField ipUPrime ( UP2M.component(tensor::XY) );
  
  const volVectorField gradIPUPrime(fvc::grad(ipUPrime));// 对uv求梯度

  const volScalarField ipUUPrime( U & gradIPUPrime );//速度U·uv梯度

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Ck",
	      ipUUPrime.mesh().time().timeName(),
	      ipUUPrime.mesh()
	   ),
	  (-1.0)*ipUUPrime,
	  ipUUPrime.boundaryField().types()
       )
   );

}


// returns the production term 产生项P_k
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::uv_Budget::Pk() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("UMean");
  
  const volSymmTensorField& UP2M = obr_.lookupObject<volSymmTensorField>("UPrime2Mean");

  const volTensorField gradU( fvc::grad(U) );
  
  const volTensorField ipUP2MGradU_( UP2M & gradU );  

  const volScalarField ipUP2MGradU = ipUP2MGradU_.component(tensor::XY) + ipUP2MGradU_.component(tensor::YX);
  
  /*
  const volScalarField gradU_1( gradU.component(tensor::XX) );
  const volScalarField gradU_2( gradU.component(tensor::YX) );
  const volScalarField gradU_3( gradU.component(tensor::ZX) );
  
  const volScalarField gradV_1( gradU.component(tensor::XY) );
  const volScalarField gradV_2( gradU.component(tensor::YY) );
  const volScalarField gradV_3( gradU.component(tensor::ZY) );
  
  const volScalarField UP2M_12( UP2M.component(tensor::XY) );
  const volScalarField UP2M_22( UP2M.component(tensor::YY) );
  const volScalarField UP2M_32( UP2M.component(tensor::YZ) );
  const volScalarField UP2M_11( UP2M.component(tensor::XX) );
  const volScalarField UP2M_13( UP2M.component(tensor::XZ) );
  const volScalarField ipUP2MGradU = gradU_2 * UP2M_22 ;
  */

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Pk",
	      ipUP2MGradU.mesh().time().timeName(),
	      ipUP2MGradU.mesh()
	   ),
	  (-1.0)*ipUP2MGradU,// 因为湍动能在上面运算的时候没有×0.5
	  ipUP2MGradU.boundaryField().types()
       )
   );
}


// returns the turbulence transport term
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::uv_Budget::Tk() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
  
  // fluctutation field
  const volVectorField UPrime = U - UMean;

  // inner product of UPrime and UPrime 速度与速度内积，得到瞬时湍动能2倍
  const volScalarField ipUPrime( UPrime.component(vector::X) * UPrime.component(vector::Y) );

  // outer product of fluctuation field 湍动能与速度乘积
  const volVectorField opUPrime( ipUPrime * UPrime );

  // divergence of opUPrime 做一次散度
  const volScalarField divOpUPrime( fvc::div(opUPrime) );

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Tk",
	      divOpUPrime.mesh().time().timeName(),
	      divOpUPrime.mesh()
	   ),
	  (-1.0)*divOpUPrime,// 因为湍动能在上面运算的时候没有×0.5
	  divOpUPrime.boundaryField().types()
       )
   );

}


// returns the viscous diffusion term //该项为粘度nu×湍动能的二阶梯度，即拉普拉斯算子
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::uv_Budget::Dk() const
{
  const volSymmTensorField& UP2M = obr_.lookupObject<volSymmTensorField>("UPrime2Mean");

  // trace of UP2M 求u'u'
  const volScalarField uvUP2M( UP2M.component(tensor::XY) );

  // laplacian of uvUP2M 做梯度运算
  const volScalarField lapUP2M( fvc::laplacian(uvUP2M) );

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Dk",
	      lapUP2M.mesh().time().timeName(),
	      lapUP2M.mesh()
	   ),
	  nu_uv*lapUP2M, //这里需要修改，原始代码中，需要用Re与nu换算
	  lapUP2M.boundaryField().types()
       )
   );
}


// returns the viscous dissipation term 耗散项 Epsilon
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::uv_Budget::Epik() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
 
  // fluctuation field
  const volVectorField UPrime = U - UMean;
  const volScalarField UPrime_1 = UPrime.component(vector::X);
  const volScalarField UPrime_2 = UPrime.component(vector::Y);
  
  // gradient of the fluctuation field 获得瞬时速度的脉动量
  const volVectorField gradUPrime_1( fvc::grad(UPrime_1) );
  const volVectorField gradUPrime_2( fvc::grad(UPrime_2) );

  // double inner product of gradUPrime 速度梯度双内积
  const volScalarField ipGradUPrime( gradUPrime_1 & gradUPrime_2 );
  
  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Epik",
	      ipGradUPrime.mesh().time().timeName(),
	      ipGradUPrime.mesh()
	   ),
	  (-2*nu_uv)*ipGradUPrime,
	  ipGradUPrime.boundaryField().types()
       )
   );
}


// returns the velocity-pressure gradient term
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::uv_Budget::Pik() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
 
  const volScalarField& P = obr_.lookupObject<volScalarField>("p");
  const volScalarField& PMean = obr_.lookupObject<volScalarField>("pMean");

  // fluctuation field
  const volVectorField UPrime = U - UMean;
  const volScalarField uPrime_1 = UPrime.component(vector::X);
  const volScalarField uPrime_2 = UPrime.component(vector::Y);
  const volScalarField PPrime = P - PMean;

  // gradient of the PPrime field
  const volVectorField gradPPrime( fvc::grad(PPrime) );
  const volScalarField gradPPrime_dpdx( gradPPrime.component(vector::X) );
  const volScalarField gradPPrime_dpdy( gradPPrime.component(vector::Y) );
  
  const volVectorField graduPrime_1( fvc::grad(uPrime_1) );
  const volVectorField graduPrime_2( fvc::grad(uPrime_2) );
  const volScalarField graduPrime_dudy( graduPrime_1.component(vector::Y) );
  const volScalarField graduPrime_dvdx( graduPrime_2.component(vector::X) );

  // double inner product of UPrime and gradPPrime
  const volScalarField ipUPrimeGradPPrime = uPrime_2*gradPPrime_dpdx + PPrime*graduPrime_dvdx + uPrime_1*gradPPrime_dpdy + PPrime*graduPrime_dudy;
  
  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Pik",
	      ipUPrimeGradPPrime.mesh().time().timeName(),
	      ipUPrimeGradPPrime.mesh()
	   ),
	  (-1.0)*ipUPrimeGradPPrime,
	  ipUPrimeGradPPrime.boundaryField().types()
       )
   );
}

// returns the redistribution term
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::uv_Budget::Phik() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
 
  const volScalarField& P = obr_.lookupObject<volScalarField>("p");
  const volScalarField& PMean = obr_.lookupObject<volScalarField>("pMean");

  // fluctuation field
  const volVectorField UPrime = U - UMean;
  const volScalarField uPrime_1 = UPrime.component(vector::X);
  const volScalarField uPrime_2 = UPrime.component(vector::Y);
  const volScalarField PPrime = P - PMean;

  // gradient of the PPrime field
  const volVectorField gradPPrime( fvc::grad(PPrime) );
  const volVectorField graduPrime_1( fvc::grad(uPrime_1) );
  const volVectorField graduPrime_2( fvc::grad(uPrime_2) );
  const volScalarField graduPrime_dudy( graduPrime_1.component(vector::Y) );
  const volScalarField graduPrime_dvdx( graduPrime_2.component(vector::X) );

  // double inner product of UPrime and gradPPrime
  const volScalarField ipUPrimeGradPPrime = PPrime*graduPrime_dvdx + PPrime*graduPrime_dudy;
  
  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Phik",
	      ipUPrimeGradPPrime.mesh().time().timeName(),
	      ipUPrimeGradPPrime.mesh()
	   ),
	  ipUPrimeGradPPrime,
	  ipUPrimeGradPPrime.boundaryField().types()
       )
   );
}

// ************************************************************************* //
